<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共{{total}}条记录</span>
          <el-button
          @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round>
            添加面经
          </el-button>
        </div>
      </template>
      <!--
        el-table 属性
        data:表格数据 必须是一个 数组
        el-table-column
        width:宽度，如果不设置表示自适应
        lable：表头的文字
        prop：字符串类型，要在此列渲染的数据属性名
       -->
     <el-table
       :data="list"
      style="width: 100%">
       <el-table-column
       prop="stem"
      label="标题"
      width="400">
    </el-table-column>
      <el-table-column
      prop="creator"
      label="作者"
      width="120">
    </el-table-column>
    <el-table-column
      prop="likeCount"
      label="点赞"
      width="120">
    </el-table-column>
    <el-table-column
      prop="views"
      label="浏览数"
      width="120">
    </el-table-column>
    <el-table-column
      prop="createdAt"
      label="更新时间"
      width="120">
    </el-table-column>
    <el-table-column
      label="操作"
      width="120">
      <template #default={row}>
        <div class="actions">
          <i @click="openDrawer('preview',row.id)" class="el-icon-view"></i>
          <i @click="openDrawer('edit',row.id)" class="el-icon-edit"></i>
          <i @click="del(row.id)" class="el-icon-delete"></i>
        </div>
      </template>
    </el-table-column>

    </el-table>
<el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="q.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="q.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <el-drawer
    size='50%'
  :title="drawerTitle"
  :visible.sync="drawer"
  direction="rtl"
  :before-close="handleClose">
  <div v-if="drawerType === 'preview'" class="article-preview">
  <h5>{{ form.stem }}</h5>
  <div v-html="form.content"></div>
</div>
  <el-form v-else ref="form" :model="form" :rules="rules" label-width="80px">
  <el-form-item label="标题" prop="stem">
    <el-input v-model="form.stem"></el-input>
  </el-form-item>
  <el-form-item label="内容" prop="content">
    <!-- 对富文本编辑器进行单独的表单校验 -->
  <quillEditor v-model="form.content" @blur="$refs.form.validateField('content')" />
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submit">确认</el-button>
    <el-button>取消</el-button>
  </el-form-item>
  </el-form>
</el-drawer>
  </div>
</template>

<script>
import { addArticles, delArticles, getArticleDetail, getArticles, updataArticle } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      // 收集表单数据对象
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      q: {
        current: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      drawer: false,
      drawerType: 'add'
    }
  },
  created () {
    this.loadArticles()
  },
  computed: {
    drawerTitle () {
      let title = '添加面经'
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'preview') title = '面经预览'
      if (this.drawerType === 'edit') title = '面经编辑'

      return title
    }
  },

  // this.tableData = res.data.rows

  methods: {
    async submit () {
      try {
        // 兜底校验
        await this.$refs.form.validate()
        // 1.发送ajax请求
        let res = null
        if (this.drawerType === 'add') {
          res = await addArticles(this.form)
        } else {
          const { id, stem, content } = this.form
          res = await updataArticle({ id, stem, content })
        }
        // 2.提示用户
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 3. 重新渲染页面
        // 将页码跳转到第1页
        this.q.current = 1
        this.loadArticles()
        // 4. 关闭抽屉
        this.handleClose()

        // console.log(res)
        // console.log('验证通过')
      } catch (error) {
        console.log(error)
      }
    },
    handleClose (done) {
      // 重置表单 ==> resetFields() 重置表单的校验规则 重置表单中的数据
      // 注意resetFields() 只能重置 el-form-item中带有prop="stem"属性的表单项
      if (this.drawerType !== 'preview') {
        this.$refs.form.resetFields()
      }

      // 手动重置数据
      this.form = {
        stem: '',
        content: ''
      }
      //  关闭抽屉组件
      this.drawer = false
    },
    async openDrawer (type, id) {
      console.log(type, id)
      this.drawer = true
      this.drawerType = type
      // 只要不是新增 其他预览或者编辑 都需要获取当前id对应的数据填充到表单
      if (type !== 'add') {
        // f发送请求 ，数据回显
        const res = await getArticleDetail(id)
        // console.log(res);
        this.form = res.data
      }
    },
    async loadArticles () {
      const res = await getArticles(this.q)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.q.pageSize = val
      this.q.current = 1
      this.loadArticles()
    },
    handleCurrentChange (val) {
      this.q.current = val
      // 重新调用方法发送ajax
      this.loadArticles()
    },
    async del (id) {
      // console.log(id)
      // 1.发送请求
      const res = await delArticles(id)
      // console.log(res)
      // 2. 提示用户
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 3. 重新渲染
      this.loadArticles()
    }

  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
